/*
 * @Author: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @Date: 2024-04-03 17:21:38
 * @LastEditors: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @LastEditTime: 2024-04-03 17:42:17
 * @FilePath: \react全栈\vue_ts-server\model\article.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const Sequelize = require('sequelize');
const sequelize = require('./init');
const UsersModel = require('./users');

const ArticleModel = sequelize.define('article', {
    // 文章ID
    id: {
        // 数据类型
        type: Sequelize.INTEGER,
        // 主键
        primaryKey: true,
        // 自增
        autoIncrement: true
    },
    // 文章标题
    title: {
        type: Sequelize.STRING(255),
        allowNull: false
    },
    // 文章内容
    content: {
        type: Sequelize.TEXT,
        allowNull: false
    },
    // 发布时间
    publish_time: {
        type: Sequelize.DATE,
        get() {
            return this.getDataValue('create_time')
                ? moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss')
                : null;
        }
    },
    update_time: {
        type: Sequelize.DATE,
        get() {
            return this.getDataValue('update_time')
                ? moment(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss')
                : null;
        }
    },
    // 发布人
    user_id: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    // 赞
    likes: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    // 踩
    dislikes: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    }
});

//删除
ArticleModel.delArticle = async function (article_id) {
    const t = await sequelize.transaction();
    try {
        await ArticleModel.destroy({
            where: { id: article_id }
        });
        t.commit();
        return true;
    } catch (e) {
        t.rollback();
        return false;
    }
}
// 添加
ArticleModel.addArticle = async function (data) {
    const t = await sequelize.transaction();
    try {
        await ArticleModel.create(data);
        t.commit();
        return true;
    } catch (e) {
        t.rollback();
        return false;
    }
}
// 修改
ArticleModel.updateArticle = async function (article_id, data) {
    const t = await sequelize.transaction();
    try {
        // 过滤掉未定义或为空的字段
        const fieldsToUpdate = ['title', 'content', 'likes', 'dislikes'];
        let updateData = {};
        fieldsToUpdate.forEach(field => {
            if (data.hasOwnProperty(field) && data[field] !== undefined) {
                updateData[field] = data[field];
            }
        });

        // 如果有字段需要更新，则添加更新时间
        if (Object.keys(updateData).length > 0) {
            updateData.update_time = new Date(); 
        }

        if (Object.keys(updateData).length > 0) {
            await ArticleModel.update(updateData, {
                where: { id: article_id },
                transaction: t
            });
        }

        await t.commit();
        return true;
    } catch (e) {
        await t.rollback();
        return false;
    }
}
// 建立关联
UsersModel.hasMany(ArticleModel, {
    foreignKey: 'user_id'
});
ArticleModel.belongsTo(UsersModel, {
    foreignKey: 'user_id'
});


module.exports = ArticleModel;